package selcoursesys.dao.impl;

import db.MyHibernateSessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import selcoursesys.dao.StudentDAO;
import selcoursesys.entity.*;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Lpbdz on 2017/4/9.
 */
public class StudentDAOImpl extends BaseHibernateDAOImpl implements StudentDAO {
    private Session session;
    public boolean deleteStudent(StudentEntity studentEntity){
        return false;
    }
    public boolean updateStudent(StudentEntity studentEntity){
        return false;
    }
    public boolean insertStudent(StudentEntity studentEntity){
        return false;
    }
    public List<StudentEntity> queryStudent(StudentEntity studentEntity){
        return null;
    }
    public StudentEntity checkStudent(StudentEntity studentEntity){
        session= MyHibernateSessionFactory.getSession();
        session.clear();
        try{
            String hql="From StudentEntity student where student.stuid=:stuid";
            Query query=session.createQuery(hql);
            query.setString("stuid",studentEntity.getStuid());
            List<StudentEntity> list=query.list();
            Iterator iterator=list.iterator();
            while(iterator.hasNext()){
                StudentEntity studentEntity1=(StudentEntity)iterator.next();
                return studentEntity1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return null;
    }

    public List<StudentEntity> queryStudentId(String stuid){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        String hql="From StudentEntity student where student.stuid=:stuid";
        Query query=session.createQuery(hql);
        query.setString("stuid",stuid);
        List list=null;
        list=query.list();
        MyHibernateSessionFactory.closeSession();
        return list;
    }
    //GET 学生专业
    public String queryStudentMajor(String stuid){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        String hql="From StudentEntity student where student.stuid=:stuid";
        Query query=session.createQuery(hql);
        query.setString("stuid",stuid);
        List list=null;
        list=query.list();
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            StudentEntity studentEntity=(StudentEntity)iterator.next();
            MyHibernateSessionFactory.closeSession();
            return studentEntity.getMajorByMajorid().getMajorid();
        }
        MyHibernateSessionFactory.closeSession();
        return null;
    }
}
